
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Data for Table_A3
data = {'Effort': [0.179, -0.153, 0.172, -0.125], 'Reliable': [0.206, -0.1, 0.053, -0.109], 'Responsive': [0.096, -0.2, -0.066, 0.01], 'Experience': [0.239, -0.135, -0.094, -0.063], 'Creative': [0.214, -0.1, 0.017, -0.019], 'Trustworthy': [0.217, -0.112, -0.137, -0.073], 'Approach': [0.149, -0.08, -0.085, -0.077], 'Confident': [0.193, -0.183, -0.014, -0.096], 'Empathy': [0.173, -0.191, 0.173, -0.19], 'Overall': [0.175, -0.1, -0.004, -0.05]}

# Standard errors
std_errors = {'Effort': [0.07, 0.058, 0.065, 0.071], 'Reliable': [0.076, 0.06, 0.073, 0.08], 'Responsive': [0.079, 0.064, 0.081, 0.094], 'Experience': [0.102, 0.081, 0.104, 0.111], 'Creative': [0.1, 0.069, 0.093, 0.106], 'Trustworthy': [0.08, 0.067, 0.101, 0.079], 'Approach': [0.067, 0.063, 0.069, 0.074], 'Confident': [0.064, 0.058, 0.065, 0.069], 'Empathy': [0.082, 0.06, 0.08, 0.08], 'Overall': [0.065, 0.053, 0.068, 0.085]}

# Independent variable labels
iv_labels = ['Public Service Motivation', 'Cognitive Ability', 'Grit Scale', 'Big 5 Personality Index']

# Convert the data and standard errors to DataFrames
df = pd.DataFrame(data, index=iv_labels)
df_se = pd.DataFrame(std_errors, index=iv_labels)

# Function to calculate T-values from coefficients and standard errors
def calculate_t_values(df, df_se):
    return df / df_se

# Calculate T-values
t_values_df = calculate_t_values(df, df_se)

# Generate significance annotations based on T-values
annotations = t_values_df.applymap(lambda x: '***' if abs(x) > 2.576 else '**' if abs(x) > 1.96 else '*' if abs(x) > 1.645 else '')

# Plotting the heatmap
plt.figure(figsize=(12, 6))

# Plot the heatmap with significance annotations and black-and-white gradient
ax = sns.heatmap(t_values_df.astype(float), annot=annotations, fmt="", cmap="Greys", center=0,
                 linewidths=0.5, cbar_kws={'label': 'T-values', 'orientation': 'horizontal'}, mask=t_values_df.isnull())

# Adjust labels and angles
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.set_xticklabels(t_values_df.columns, rotation=45, ha="center", fontsize=10)
ax.set_yticklabels(t_values_df.index, rotation=0, fontsize=10)

# Position the color bar (legend) at the bottom
cbar = ax.collections[0].colorbar
cbar.ax.xaxis.set_label_position('bottom')
cbar.ax.xaxis.set_ticks_position('bottom')

# Adjust layout
plt.tight_layout()
plt.show()
